﻿package frEngine.shader.filters.fragmentFilters
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import com.gengine.resource.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.registType.*;

    public class UVMaskFilter extends FilterBase
    {
        public var uvOffsetAndRotateParams:Vector.<Number>;
        public var uvCenterAndConst:Vector.<Number>;
        public var maskAlpha:Vector.<Number>;
        private var _maskTexture3D:Texture3D;
        private var _showMaskEnabled:Boolean = false;
        public var openSolubility:Boolean = false;

        public function UVMaskFilter()
        {
            this.uvOffsetAndRotateParams = this.Vector.<Number>([0, 0, 1, 0]);
            this.uvCenterAndConst = this.Vector.<Number>([0.5, 0.5, 0.33, 1]);
            this.maskAlpha = this.Vector.<Number>([1, 0, 1, 1]);
            super(FilterType.UVMask, FilterPriority.UVMaskFilter);
            return;
        }// end function

        override public function dispose() : void
        {
            this.uvOffsetAndRotateParams[0] = 0;
            this.uvOffsetAndRotateParams[1] = 0;
            this.uvOffsetAndRotateParams[2] = 1;
            this.uvOffsetAndRotateParams[3] = 0;
            this.uvCenterAndConst[0] = 0.5;
            this.uvCenterAndConst[1] = 0.5;
            this.uvCenterAndConst[2] = 0.33;
            this.uvCenterAndConst[3] = 1;
            this.maskAlpha[0] = 1;
            this.maskAlpha[1] = 0;
            this.maskAlpha[2] = 1;
            this.maskAlpha[3] = 1;
            if (this._maskTexture3D)
            {
                this._maskTexture3D.dispose();
                this._maskTexture3D = null;
            }
            super.dispose();
            return;
        }// end function

        override public function setRegisterParams(param1:Program3dRegisterInstance) : void
        {
            var _loc_2:* = param1.getParamByName("{uvCenterAndConst}", false);
            if (!_loc_2)
            {
                return;
            }
            _loc_2.value = this.uvCenterAndConst;
            param1.getParamByName("{uvOffsetAndRotateParams}", false).value = this.uvOffsetAndRotateParams;
            param1.getParamByName("{UVMaskTexture}", false).value = this.maskTexture3D;
            param1.getParamByName("{maskAlpha}", false).value = this.maskAlpha;
            return;
        }// end function

        override public function get programeId() : String
        {
            if (this._maskTexture3D == null || !this._maskTexture3D.loadedAndUpload)
            {
                return null;
            }
            if (this.openSolubility)
            {
                return "uvMask " + FilterBase.ATLFormat[this._maskTexture3D.format] + ",mip:" + this._maskTexture3D.mipMode + ",mask:" + this._showMaskEnabled + ",open:" + this.openSolubility;
            }
            return "uvMask " + FilterBase.ATLFormat[this._maskTexture3D.format] + ",mip:" + this._maskTexture3D.mipMode + ",mask:" + this._showMaskEnabled;
        }// end function

        public function set showMaskEnabled(param1:Boolean) : void
        {
            this._showMaskEnabled = param1;
            return;
        }// end function

        public function get showMaskEnabled() : Boolean
        {
            return this._showMaskEnabled;
        }// end function

        public function changeUVMaskValue(param1:Number, param2:Number, param3:Number) : void
        {
            this.uvOffsetAndRotateParams[0] = param1;
            this.uvOffsetAndRotateParams[1] = param2;
            var _loc_4:* = param3 * Math.PI / 180;
            this.uvOffsetAndRotateParams[2] = Math.cos(_loc_4);
            this.uvOffsetAndRotateParams[3] = Math.sin(_loc_4);
            return;
        }// end function

        public function changeAlpha(param1:Number) : void
        {
            this.maskAlpha[0] = param1;
            return;
        }// end function

        public function set maskTexture3D(param1:Texture3D) : void
        {
            this._maskTexture3D = param1;
            return;
        }// end function

        public function get maskTexture3D() : Texture3D
        {
            return this._maskTexture3D;
        }// end function

        override public function createFragmentColor(param1:Program3dRegisterInstance, param2:Boolean) : String
        {
            var _loc_5:Array = null;
            var _loc_6:String = null;
            if (!this.maskTexture3D)
            {
                return "";
            }
            var _loc_3:String = "";
            var _loc_4:* = param1.getParamByName(FilterName.V_UvOriginalPos, true);
            if (_loc_4)
            {
                _loc_5 = new Array();
                _loc_6 = _loc_4.paramName;
                _loc_5.push(new ToBuilderInfo("fc0", "{uvCenterAndConst}", false, 1, this.uvCenterAndConst));
                _loc_5.push(new ToBuilderInfo("fc1", "{uvOffsetAndRotateParams}", false, 1, this.uvOffsetAndRotateParams));
                _loc_5.push(new ToBuilderInfo("fs0", "{UVMaskTexture}", false, 1, this.maskTexture3D));
                _loc_5.push(new ToBuilderInfo("fc2", "{maskAlpha}", false, 1, this.maskAlpha));
                _loc_3 = _loc_3 + ("sub\tft0.xy\t" + _loc_6 + ".xy\tfc0.xy\t\n");
                _loc_3 = _loc_3 + "mul ft0.xy    ft0.xy   fc0.ww \n";
                _loc_3 = _loc_3 + "sub\tft0.xy\tft0.xy\tfc1.xy\t\n";
                _loc_3 = _loc_3 + "mul\tft1.xyzw\tft0.xyyx\tfc1.zwzw\t\n";
                _loc_3 = _loc_3 + "sub\tft2.x\tft1.x\tft1.y\t\n";
                _loc_3 = _loc_3 + "add\tft2.y\tft1.z\tft1.w\t\n";
                _loc_3 = _loc_3 + "add\tft2.xy\tfc0.xy\tft2.xy\t\n";
                if (!param2)
                {
                    _loc_3 = _loc_3 + "sge\tft3.xy \tft2.xy\t{fcConst1}.xx\t\n";
                    _loc_3 = _loc_3 + "slt\tft3.zw\tft2.xy\t{fcConst1}.zz\t\n";
                    _loc_3 = _loc_3 + "mul\tft3.xy\tft3.xy\tft3.zw\t\n";
                    _loc_3 = _loc_3 + "mul\tft3.x\tft3.x\tft3.y\t\n";
                    _loc_3 = _loc_3 + ("tex\tft4\t\tft2.xy\t\t" + getSmapleString("fs0", this.maskTexture3D.format, param2, 0) + "\t\n");
                    _loc_3 = _loc_3 + "mul\tft4\t\tft4\t\tft3.x \n";
                }
                else
                {
                    _loc_3 = _loc_3 + ("tex\tft4\t\tft2.xy\t\t" + getSmapleString("fs0", this.maskTexture3D.format, param2, 0) + "\t\n");
                }
                _loc_3 = _loc_3 + "mul     ft4.xyz   ft4.xyz   fc2.x  \n";
                if (this.maskTexture3D.format == TextureFormat.FORMAT_COMPRESSED_ALPHA)
                {
                    _loc_3 = _loc_3 + "mul\t\t\tft4.xyz\t   ft4.xyz\t\t\tft4.w\t \n";
                }
                if (this._showMaskEnabled)
                {
                    _loc_3 = _loc_3 + "mov\t{output}\tft4\t\t\n";
                }
                else
                {
                    _loc_3 = _loc_3 + "dp3\tft4.w\t\tft4\t\t\tfc0.z \t\n";
                    if (this.openSolubility)
                    {
                        _loc_3 = _loc_3 + "sge     ft4.w     ft4.w     fc2.y  \n";
                    }
                    _loc_3 = _loc_3 + "mul\t{output}\t{output}\tft4.w \t\n";
                }
                _loc_3 = param1.toBuild(_loc_3, _loc_5);
            }
            return _loc_3;
        }// end function

    }
}
